{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<div class="p-3">
    <form class="layui-form gg-form-bar border-x border-t" lay-filter="barsearchform">
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">订货号</label>
            <div class="layui-input-inline">
                <input name="buyCode" value="" placeholder="请输入订货号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-inline">
                <input name="name" value="" placeholder="请输入名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">规格型号</label>
            <div class="layui-input-inline">
                <input name="specs" value="" placeholder="请输入规格型号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">品牌</label>
            <div class="layui-input-inline">
                <input name="brand" value="" placeholder="请输入品牌" class="layui-input">
            </div>
        </div>
        <div class="layui-input-inline" style="width:300px;">
            <input type="text" class="layui-input"  placeholder="合同号"  name="htOrderNum">
        </div>
        <div class="layui-input-inline" style="width:150px;">
            <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
            <button type="reset" class="layui-btn layui-btn-reset" lay-filter="reset">清空</button>
        </div>
    </form>
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <span class="layui-btn layui-btn-sm" lay-event="onSure">确认</span>
    </div>
</script>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
    const moduleInit = ['tool','laydatePlus'];
    function gouguInit() {
        var form = layui.form,table = layui.table,tool=layui.tool, laydatePlus = layui.laydatePlus;
        //日期范围
        var diff_time = new laydatePlus({'target':'diff_time'});

        //监听搜索提交
        form.on('submit(webform)', function(data) {
            layui.pageTable.reload({
                where: data.field,
                page: {curr: 1}
            });
            return false;
        });

        layui.pageTable = table.render({
            elem: '#test',
            title: '发票列表',
            toolbar: '#toolbarDemo',
            method:'post',
            defaultToolbar:[],
            url: "{:url('api/getCgHtMx')}?punterId={$punterId}&mxIds={$mxIds}", //数据接口
            page: true, //开启分页
            cellMinWidth: 80,
            limit: 20,
            cols: [
                [
                    {type: 'checkbox', fixed: 'left' },
                     // {field: 'mdName', title: '名称'}
                    {field: 'brand', title: '品牌'}
                    , {field: 'specs', title: '规格型号'}
                    // , {field: 'unit', title: '单位'}
                    , {field: 'buyCode', title: '订货号'},
                    {
                        field: 'htNum',
                        title: '合同编号',
                        align: 'right',
                    },
                    {
                        field: 'bjMoney',
                        title: '采购金额',
                        align: 'center',

                    }, {
                        field: 'bjNum',
                        title: '采购数量',
                        align: 'center',

                    }, {
                        field: 'bjPrice',
                        title: '采购单价',
                        align: 'center',

                    },
                    {
                    field: 'dhMoney',
                    title: '已到货金额',
                    align: 'center',
                    },
                    {
                    field: 'dhUnpayMoney',
                    title: '已到货未付款金额',
                    align: 'center',
                    },
                    {
                    field: 'payedMoney',
                    title: '已付款金额',
                    align: 'center',
                    },
                    {
                    field: 'unPayMoney',
                    title: '未付款金额',
                    align: 'center',
                    },

                    // ,{
                    // 	field: 'check_user',
                    // 	title: '当前审核人',
                    // 	width: 140
                    // }

                ]
            ]
        });

        //表头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            if (obj.event === 'onSure') {
                var data = checkStatus.data;
                if (data.length<=0){
                    layer.msg('请选择数据');
                    return;
                }else {
                    parent.saveMx(data);
                    return;
                }
                return;
            }
            if(obj.event === 'LAYTABLE_EXCEL'){
                var formSelect = form.val('barsearchform');
                formSelect.limit=99999;
                $.ajax({
                    url: 'index',
                    data: formSelect,
                    success:function(res){
                        table.exportFile('test', res.data,'xls');
                    }
                });
                return;
            }
        });
        //监听行工具事件
        table.on('tool(test)', function(obj) {
            var data = obj.data;
            if (obj.event === 'view') {
                tool.side("view?id="+data.id);
                return;
            }
            if (obj.event === 'edit') {
                tool.side("add?id="+data.id);
                return;
            }
            if (obj.event === 'del') {
                layer.confirm('确定删除该发票记录吗？', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    $.ajax({
                        url: "delete",
                        type:'post',
                        data: {id: data.id},
                        success: function(res) {
                            layer.msg(res.msg);
                            if (res.code == 0) {
                                obj.del();
                            }
                        }
                    })
                    layer.close(index);
                });
            }
            if(obj.event === 'open'){
                layer.open({
                    type: 1
                    ,title: '标记为已开票'
                    ,area: '368px;'
                    ,id: 'LAY_module' //设定一个id，防止重复弹出
                    ,btn: ['确定', '取消']
                    ,btnAlign: 'c'
                    ,content: '<div style="padding-top:15px;">\
								<div class="layui-form-item">\
								  <label class="layui-form-label">发票号码</label>\
								  <div class="layui-input-inline">\
									<input type="text" name="code" autocomplete="off" value="'+data.code+'" placeholder="请输入发票号码" class="layui-input">\
								  </div>\
								</div>\
								<div class="layui-form-item">\
								  <label class="layui-form-label">开票日期</label>\
								  <div class="layui-input-inline">\
									<input type="text" name="open_time" autocomplete="off" value="'+data.open_time+'" placeholder="请选择实际开票日期" class="layui-input tool-time">\
								  </div>\
								</div>\
								<div class="layui-form-item">\
								  <label class="layui-form-label">物流单号</label>\
								  <div class="layui-input-inline">\
									<input type="text" name="delivery" autocomplete="off" value="'+data.delivery+'" placeholder="请输入物流快递单号" class="layui-input">\
								  </div>\
								</div>\
							  </div>'
                    ,success:function(){

                    }
                    ,yes: function(index){
                        let code = $('#LAY_module').find('[name="code"]').val();
                        let open_time = $('#LAY_module').find('[name="open_time"]').val();
                        let delivery = $('#LAY_module').find('[name="delivery"]').val();
                        if(code == ''){
                            layer.msg('请输入发票号码');
                            return false;
                        }
                        if(open_time == ''){
                            layer.msg('请选择开票时间');
                            return false;
                        }
                        $.ajax({
                            url: "/finance/api/open",
                            type: 'post',
                            data: {
                                id: data.id,
                                code: code,
                                open_time: open_time,
                                delivery: delivery
                            },
                            success: function(e) {
                                layer.msg(e.msg);
                                if (e.code == 0) {
                                    layer.close(index);
                                    layui.pageTable.reload();
                                }
                            }
                        })
                    }
                    ,btn2: function(){
                        layer.closeAll();
                    }
                });
            }
        });
    }
</script>
{/block}
<!-- /脚本 -->
